//package work.timejay.user.utils;
//
//import com.alibaba.fastjson.JSONObject;
//import com.mysql.cj.x.protobuf.MysqlxDatatypes;
//import jdk.nashorn.internal.parser.JSONParser;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//import work.timejay.user.entity.mqs.MqLog;
//import work.timejay.user.entity.User;
//import work.timejay.user.entity.users.UserAmount;
//import work.timejay.user.service.mqs.MqLogService;
//import work.timejay.user.service.users.UserAmountService;
//import work.timejay.user.service.UserService;
//import work.timejay.user.vo.UserSend;
//
//import java.io.StringReader;
//import java.security.acl.Group;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @className:RocketMQAccpetUtils.java
// * @author:王文杰
// * @version:1.0.0
// * @description:rocketMQ接受消息工具类型
// * @createTime 2022年01月02日 15:47:00
// */
//@Slf4j
//@Component
//@RocketMQTransactionListener(txProducerGroup = "my_tx_group")
//public class RocketMQAccpetUtils implements RocketMQLocalTransactionListener {
//    @Autowired
//    private UserAmountService userAmountService;
//    @Autowired
//    private MqLogService mqLogService;
//
//    /**
//     * 执行本地事务
//     * @param msg
//     * @param arg
//     * @return
//     */
//    @Override
//    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
//        log.info("msg,arg----->{}-{}",msg,arg);
//        try {
//            String bytes = new String((byte[]) msg.getPayload());
//            JSONObject jsonObject = JSONObject.parseObject(bytes);
//            log.info("jsonObject---->{}",jsonObject);
//            Object message = jsonObject.get("message");
//            UserSend userSend = JSONObject.parseObject(message.toString(), UserSend.class);
//            //扣款 TODO 加本地事务
//            userAmountService.updateAmount(userSend);
//            log.info("【本地业务执行完毕】 msg:{}, Object:{}", msg, arg);
//            //返回Commit
//            return RocketMQLocalTransactionState.COMMIT;
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.info("【本地业务执行异常】 msg:{}, Object:{}", msg, arg);
//            //TODO 回写
//            //返回Rollback
//            return RocketMQLocalTransactionState.ROLLBACK;
//        }
//    }
//
//    /**
//     * 校验本地事务，一分钟未返回，开始事务会查，默认执行5次
//     * @param msg
//     * @return
//     */
//    @Override
//    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
//        log.info("【执行检测任务】 msg:{}", msg);
//        String bytes = new String((byte[]) msg.getPayload());
//        JSONObject jsonObject = JSONObject.parseObject(bytes);
//        Object message = jsonObject.get("message");
//        UserSend userSend = JSONObject.parseObject(message.toString(), UserSend.class);
//        //幂等性
//        MqLog one = mqLogService.lambdaQuery().eq(MqLog::getTxNo, userSend.getTxNo()).one();
//        if(null != one){
//            return RocketMQLocalTransactionState.COMMIT;
//        }
//        return RocketMQLocalTransactionState.UNKNOWN;
//    }
//}
